package com.haiwei.poi.excel.common;

import com.haiwei.poi.common.json.HaiweiJsonUtil;
import com.haiwei.poi.word.bean.HaiweiBeanCallback;
import com.haiwei.poi.word.bean.HaiweiBeanUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * haiweiTableBeanUtil测试类.
 *
 * @date: 2022-06-24
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiBeanUtilTester {
    
    @Test
    public void setStringTest(){
        HaiweiBeanUtil.setString("22222","order2.key1");
        HaiweiBeanUtil.print();
        Assert.assertEquals("22222",HaiweiBeanUtil.getValue("order2.key1"));
    
        // 覆盖场景
        HaiweiBeanUtil.setString("333","order2.key1");
        HaiweiBeanUtil.print();
        Assert.assertEquals("333",HaiweiBeanUtil.getValue("order2.key1"));
        
        // 添加新路径
        HaiweiBeanUtil.setString("444","order2.key2");
        HaiweiBeanUtil.setString("555","order3.key1");
        HaiweiBeanUtil.print();
        Assert.assertEquals("444",HaiweiBeanUtil.getValue("order2.key2"));
        Assert.assertEquals("555",HaiweiBeanUtil.getValue("order3.key1"));
    }
    
    @Test
    public void setJsonTest(){
        HaiweiBeanUtil.setJson("{name1:1111,age1:123}","order2.order23");
        HaiweiBeanUtil.print();
        Assert.assertEquals("1111",HaiweiBeanUtil.getValue("order2.order23.name1"));
        Assert.assertEquals("123",HaiweiBeanUtil.getValue("order2.order23.age1"));
    
        HaiweiBeanUtil.setJson("{name3:1111,age3:123}","order3");
        HaiweiBeanUtil.print();
        Assert.assertEquals("1111",HaiweiBeanUtil.getValue("order3.name3"));
        Assert.assertEquals("123",HaiweiBeanUtil.getValue("order3.age3"));
    
        HaiweiBeanUtil.setJson("{name3:1111,age3:123}","order2");
        HaiweiBeanUtil.print();
        Assert.assertEquals("1111",HaiweiBeanUtil.getValue("order2.name3"));
        Assert.assertEquals("123",HaiweiBeanUtil.getValue("order2.age3"));
    
        List<Item1> items = new ArrayList<>();
        items.add(new Item1("鼠标1","电子类1","2018/8/26"));
        items.add(new Item1("鼠标2","电子类2","2018/8/27"));
        HaiweiBeanUtil.setJson(HaiweiJsonUtil.toJson(items),"order2.item");
        HaiweiBeanUtil.print();
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("order2.item.0.name"));
        Assert.assertEquals("电子类2",HaiweiBeanUtil.getValue("order2.item.1.type"));
    
        Map<String, Object> map = new HashMap<>();
        List<Item1> items1 = new ArrayList<>();
        items1.add(new Item1("鼠标1","电子类1","2018/8/26"));
        items1.add(new Item1("鼠标2","电子类2","2018/8/27"));
        map.put("items",items1);
    
        HaiweiBeanUtil.setJson(HaiweiJsonUtil.toJson(map),"root.order11");
        HaiweiBeanUtil.print();
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.order11.items.0.name"));
        Assert.assertEquals("电子类2",HaiweiBeanUtil.getValue("root.order11.items.1.type"));
    }
    
    @Test
    public void setBeanTest(){
        final Item1 item1 = new Item1("鼠标1", "电子类1", "2018/8/26");
        HaiweiBeanUtil.setBean(item1,"root");
        HaiweiBeanUtil.print();
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.name"));
    
        HaiweiBeanUtil.setBean(item1,"root.order1");
        HaiweiBeanUtil.print();
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.order1.name"));
    
        List<Item1> items = new ArrayList<>();
        items.add(new Item1("鼠标1","电子类1","2018/8/26"));
        items.add(new Item1("鼠标2","电子类2","2018/8/27"));
        HaiweiBeanUtil.setBean(items,"root.items");
        HaiweiBeanUtil.print();
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.items.0.name"));
        Assert.assertEquals("电子类2",HaiweiBeanUtil.getValue("root.items.1.type"));
    
        Order1 order1 = new Order1("特仑苏1","N0001",items);
        Order1 order2 = new Order1("特仑苏2","N0002",items);
        List<Order1> orders = new ArrayList<>();
        orders.add(order1);
        orders.add(order2);
        HaiweiBeanUtil.setBean(orders,"root.wuqiang.orders");
        HaiweiBeanUtil.print();
        Assert.assertEquals("特仑苏1",HaiweiBeanUtil.getValue("root.wuqiang.orders.0.name"));
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.wuqiang.orders.0.items.0.name"));
        Assert.assertEquals("鼠标2",HaiweiBeanUtil.getValue("root.wuqiang.orders.0.items.1.name"));
        
        Assert.assertEquals("N0002",HaiweiBeanUtil.getValue("root.wuqiang.orders.1.no"));
        Assert.assertEquals("鼠标1",HaiweiBeanUtil.getValue("root.wuqiang.orders.1.items.0.name"));
        Assert.assertEquals("鼠标2",HaiweiBeanUtil.getValue("root.wuqiang.orders.1.items.1.name"));
        Assert.assertEquals(2,HaiweiBeanUtil.getCollectionLen("root.wuqiang.orders"));
        Assert.assertEquals(2,HaiweiBeanUtil.getCollectionLen("root.wuqiang.orders.0.items"));
    
        Map<String, Object> map = new HashMap<>();
        List<Item1> items3 = new ArrayList<>();
        items3.add(new Item1("鼠标1","电子类1","2018/8/26"));
        items3.add(new Item1("鼠标2","电子类2","2018/8/27"));
        map.put("items",items3);
        HaiweiBeanUtil.setBean(map);
        HaiweiBeanUtil.print();
        Assert.assertEquals(2,HaiweiBeanUtil.getCollectionLen("items"));
        
    }
    
    @Test
    public void cleanTest(){
        final Item1 item1 = new Item1("鼠标1", "电子类1", "2018/8/26");
        HaiweiBeanUtil.setBean(item1,"root");
        HaiweiBeanUtil.print();
        HaiweiBeanUtil.clean("root.name");
        HaiweiBeanUtil.print();
        HaiweiBeanUtil.clean("root");
        HaiweiBeanUtil.print();
    }
    
    /**
     * getValue方法
     * 没有获取到节点场景 测试
     * 逻辑：没有获取到节点，如果回调存在就调用回调函数
     */
    @Test
    public void getValueEmptyCaseTest(){
    
        HaiweiBeanUtil.setCallBackCache(new HaiweiBeanCallback() {
            @Override
            public Object getBean(String path) {
                return null;
            }
        });
    }
    
    @Data
    @AllArgsConstructor
    static class Item1{
        private String name = "鼠标";
        private String type = "电子类";
        private String time = "2018/8/26";
    }
    
    @Data
    @AllArgsConstructor
    static class Order1{
        private String name = "订单名称";
        private String no = "订单编号";
        private List<Item1> items = new ArrayList<>();
    }
    
}
